<?php

namespace common\entities\config;

use common\entities\common\MyscrmOrganizationEntity;
use common\entities\ConfigEntity;
use common\error\ErrorCode;
use Yii;
use yii\db\Exception;

/**
 * This is the model class for table "myscrm_functionupgrade".
 *
 * @property string $functionUpgradeId
 * @property string $organizationId
 * @property string $functionName
 * @property integer $functionMode
 * @property MyscrmOrganizationEntity $organization
 */
class MyscrmFunctionupgradeEntity extends ConfigEntity
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'myscrm_functionupgrade';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['functionUpgradeId'], 'required'],
            [['functionMode'], 'integer'],
            [['functionUpgradeId', 'organizationId'], 'string', 'max' => 36],
            [['functionName'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'functionUpgradeId' => 'Function Upgrade ID',
            'organizationId' => 'Organization ID',
            'functionName' => 'Function Name',
            'functionMode' => 'Function Mode',
        ];
    }


    /**
     * 根据organizationId获取数据
     * @param string $organizationId
     * @return array|bool|\yii\db\ActiveRecord[]
     */
    public function getDataByOrganizationId($organizationId)
    {
        try
        {
            return self::find()->filterWhere(['organizationId' => $organizationId])->asArray()->all();

        }catch (Exception $e)
        {
            return self::setAndReturn(ErrorCode::ERR_QMYX_CSSZ_ACTIVE_RECORD_EXCEPTION,$e->getMessage());
        }
    }

    /**
     * 根据根据organizationId和FunctionName获取数据
     * @param $organizationId
     * @param $functionName
     * @return array|bool|\yii\db\ActiveRecord[]
     */
    public function getOrgFunctionMode($organizationId, $functionName){
        try
        {
            return self::find()->where(['organizationId' => $organizationId,'FunctionName' => $functionName])->asArray()->all();

        }catch (Exception $e)
        {
            return self::setAndReturn(ErrorCode::ERR_QMYX_CSSZ_ACTIVE_RECORD_EXCEPTION,$e->getMessage());
        }
    }

    public function getOrganization()
    {
        return $this->hasOne(MyscrmOrganizationEntity::className(), ['Id' => 'organizationId']);
    }
}
